<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="仓库属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag
                type="radio"
                v-model="model.type"
                dictCode="wms_warehouse_type"
                placeholder="请选择仓库属性"
                @change="typeChange"
              />
            </a-form-model-item>
          </a-col>
          <template v-if="model.type === '1'">
            <a-col :span="24">
              <a-form-model-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                <a-input v-model="model.name" placeholder="请填写仓库名称" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="仓库代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
                <a-input v-model="model.code" placeholder="请输入仓库代码，若不填写仓库代码则由系统生成" :maxLength="6" :disabled="model.id && !formDisabled"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="国家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="country">
                <j-search-select-tag
                  v-model="model.country"
                  dict="hz_logistics_country_default,cn_name,code,logistics"
                  placeholder="请选择所属国家"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item
                label="联系人"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="contacts"
                :rules="[{ required: true, message: '请输入联系人姓名' }]"
              >
                <a-input v-model="model.contacts" placeholder="请输入联系人姓名" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item
                label="联系电话"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                prop="phone"
                required
              >
              <a-input v-model="model.phone" placeholder="请输入联系电话" :maxLength="11"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipCode">
                <a-input v-model="model.zipCode" placeholder="请输入仓库邮编" :maxLength="30"></a-input>
              </a-form-model-item>
            </a-col>
          </template>
          <template v-if="model.type === '2'">
            <a-col :span="24">
              <a-form-model-item label="归属服务商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="provider">
                <j-dict-select-tag
                  type="list"
                  v-model="model.provider"
                  dictCode="wms_warehouse_provider"
                  placeholder="请选择归属服务商"
                  :disabled="model.id && !formDisabled"
                />
              </a-form-model-item>
              <a-col :span="24">
                <a-form-model-item
                  label="平台仓库代码"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="code"
                  required
                >
                  <a-input v-model="model.code" placeholder="请输入平台仓库代码" :maxLength="30" :disabled="model.id && !formDisabled"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                  <a-input v-model="model.name" placeholder="请尽可能输入仓库准确详尽地址" :maxLength="20"></a-input>
                </a-form-model-item>
              </a-col>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="国家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="country">
                <j-search-select-tag
                  v-model="model.country"
                  dict="hz_logistics_country_default,cn_name,code,logistics"
                  placeholder="请选择仓库所属国家"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city">
                <a-input v-model="model.city" placeholder="请输入城市" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="省/州" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="provincial">
                <a-input v-model="model.provincial" placeholder="请输入省/州" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipCode">
                <a-input v-model="model.zipCode" placeholder="请输入邮编" :maxLength="30"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contacts">
                <a-input v-model="model.contacts" placeholder="请输入联系人" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
                <a-input v-model="model.phone" placeholder="请输入联系电话" :maxLength="20"></a-input>
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :span="24">
            <a-form-model-item
              label="仓库地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="address"
              :rules="model.type === '1' ? { required: true, message: '请输入仓库地址' } : {}"
            >
              <a-textarea v-model="model.address" placeholder="请输入仓库地址" auto-size :maxLength="150"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" auto-size  :maxLength="150"></a-textarea>
            </a-form-model-item>
            <a-col :span="24">
              <a-form-model-item label="仓库状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
                <j-switch
                  v-model="model.status"
                  :options="['Y', 'N']"
                  checkedChildrenText="开"
                  unCheckedChildrenText="关"
                ></j-switch>
              </a-form-model-item>
            </a-col>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WarehouseForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    type:{
      type:String
    }
  },
  data() {
    return {
      model: {
        type: this.type==='2'?'2':'1', //默认系统仓
        status: 'Y',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入仓库名称' },
          {
            validator: (rule, value, callback) =>
              validateDuplicateValue('hz_wms_warehouse_info', 'name', 'wms', value, this.model.id, callback),
          },
        ],
        code: [  
          {validator:this.codeValidator},
          {
            validator: (rule, value, callback) =>
              validateDuplicateValue('hz_wms_warehouse_info', 'code', 'wms', value, this.model.id, callback),
          },
        ],
        phone:[{validator:this.phoneValidator},],
        provider: [{ required: true, message: '请选择归属服务商' }],
        country: [{ required: true, message: '请选择国家' }],
        zipCode: [
          { required: true, message: '请输入邮编' },
        ],
      },
      url: {
        add: '/wms/warehouse/add',
        edit: '/wms/warehouse/edit',
        queryById: '/wms/warehouse/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    //仓库代码校验
    codeValidator(rule, value, callback){
       if(this.model.type === '2' && !value){
         callback('请输入平台仓库代码')
       }
       validateDuplicateValue('hz_wms_warehouse_info', 'code', 'wms', value, this.model.id, callback)
    },
    //联系电话校验
    phoneValidator(rule, value, callback){  
      if(this.model.type === '1' && !value){
        callback('请填写联系电话')
      }
      if(value && !new RegExp(/^([1-9]\d*|[0]{1})$/).test(value)){
        callback('请填写整数')
      }
      else{
        callback()
      }
     
    },
    //仓库属性切换  清除表单校验信息
    typeChange(){
      this.$refs.form.clearValidate();
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }else{
          that.$message.error('创建失败，存在未填写必填项')
        }
      })
    },
  },
}
</script>